<template>
  <div class="app-container">
<!--查询条件开始-->
    <el-card class="box-card" style="margin-bottom: 10px;" >
      <div slot="header" class="clearfix">
        <span>查询条件</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="楼栋编号" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入登录名称"
            clearable
            size="small"
            style="width:150px"
          />
        </el-form-item>
        <el-form-item label="楼栋名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入登录账号"
            clearable
            size="small"
            style="width:150px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!--表头按钮开始-->
    <el-card class="box-card" style="margin-bottom: 10px;" >
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="openAddFloorPop">添加楼栋</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <!--表头按钮结束-->


    <!--数据表格开始-->
    <el-table :data="floorList" :header-cell-style="{ 'color': '#3D4144' }">
      <el-table-column label="楼栋编号" align="center" prop="id" />
      <el-table-column label="楼栋名称" align="center" prop="name" />
      <el-table-column label="建筑面积" align="center" prop="floorArea">
        <template slot-scope="scope">
          <span>{{scope.row.floorArea}}</span>㎡
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="openUpdateFloorPop(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="delFloor(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--数据表格结束-->

    <!--分页控件开始-->
    <el-pagination
      v-show="total>0"
      :current-page="queryParams.pageNum"
      :page-sizes="[5,10,20,30]"
      :page-size="queryParams.pageSize"
      layout="total,sizes,prev,pager,next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-card>
    <!--分页控件结束-->

<!--    修改弹窗-->
    <el-dialog title="修改楼栋信息" :visible.sync="updateFloorPop" width="25%">
      <Update :updateData="updateData"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateFloorPop = false">取 消</el-button>
        <el-button type="primary" @click="updateFloor">确 定</el-button>
      </span>
    </el-dialog>

<!--    添加楼栋弹窗-->
    <el-dialog title="添加楼栋" :visible.sync="addFloorPop" width="25%">
      <AddFloor :addFloorData="addFloorData"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFloorPop = false">取 消</el-button>
        <el-button type="primary" @click="addFloor">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {finFloorList,updateFloor,removeFloor,addFloorC} from '@/api/assets/floorApi'

import Update from './page/updateFloor'
import AddFloor from './page/addFloor'
export default {
  name: "floorIndex",
  components:{Update,AddFloor},
  data(){
    return{
      // 楼栋集合
      floorList:[],
      // 查询条件
      queryParams:{
        name:undefined,
        id:undefined,
        pageSize:1,
        pageNum:10
      },
      // 总条数
      total: 0,
      // 修改弹窗
      updateFloorPop: false,
      // 新增弹窗
      addFloorPop: false,
      // 修改选中的值
      updateData:{},
      // 新增楼栋属性
      addFloorData: {
        name:undefined,
        remark:undefined,
        floorArea:undefined
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods:{
    /**
     * 新增楼栋信息
     */
    addFloor(){
      addFloorC(this.addFloorData).then(res => {
        if(res.data){
          this.addFloorPop = false
          this.handleQuery()
          this.addFloorData = {
            name:undefined,
            remark:undefined,
            floorArea:undefined
          }
          this.$message({
            message: '新增成功',
            type: 'success'
          });
        }
      })
    },

    /**
     * 打开新增楼栋弹窗
     */
    openAddFloorPop(){
      this.addFloorPop = true
    },

    /**
     * 删除楼栋信息
     */
    delFloor(id){
      this.$confirm('是否删除楼栋信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeFloor(id).then(res => {
          if(res.data){
            this.handleQuery()
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else{
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /**
     * 修改楼栋信息按钮
     */
    updateFloor(){
      updateFloor(this.updateData).then(res => {
        if(res.data){
          this.updateFloorPop = false
          this.$message({
            message: '修改成功',
            type: 'success'
          });
        }
      })
    },

    /**
     * 打开修改楼栋信息弹窗
     */
    openUpdateFloorPop(floor){
      this.updateData = floor
      this.updateFloorPop = true
    },

    /**
     * 查询按钮
     */
    handleQuery(){
      const query = {
        id: this.queryParams.id,
        name : this.queryParams.name
      }
      finFloorList(query).then(res => {
        this.floorList = res.data.records
        this.total = res.data.total
      })
    },

    //页面大小发生改变
    handleSizeChange(val){
      this.queryParams.pageSize = val
      this.handleQuery()
    },

    //当前页数发生改变
    handleCurrentChange(val){
      this.queryParams.pageNum = val
      this.handleQuery()
    },
  }
}
</script>

<style scoped>

</style>
